//-----------------------------------------------------------------------
// <copyright file="AddFilter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Adds an ISAPI filter to a web site.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;

    /// <summary>
    /// Adds a new host/ip binding to the specified web site
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.WebSite.AddFilter 
    ///     MachineName="machineName" 
    ///     Identifier="identifier"
    ///     Description="description"
    ///     FilterName="filterName"
    ///     FilterPath="filterPath" />]]></code>
    /// <para>where:</para>
    /// <para><i>description</i></para>
    /// <para>Web site description to apply binding to</para>
    /// <para><i>identifier</i></para>
    /// <para>Web site identifier to apply binding to</para>
    /// <para><i>FilterName (Required)</i></para>
    /// <para>Name of new ISAPI filter</para>
    /// <para><i>FilterPath (Required)</i></para>
    /// <para>Path to new Filter executable</para>
    /// <para><i>machineName</i></para>
    /// <para>Machine to operate on(defaults to localhost)</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.WebSite.AddFilter
    ///             Desciption="mytestsite"
    ///             FilterName="Quova Filter"
    ///             FilterPath="c:\Program Files\Quova\Quova.dll"/>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class AddFilter : TaskBase
    {
        private string description;
        private string identifier;
        private string filterName;
        private string filterPath;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Name for new ISAPI Filter
        /// </summary>
        /// <value>A valid filter description</value>
        /// <remarks>required</remarks>
        public string FilterName
        {
            get { return (this.filterName ?? String.Empty); }
            set { this.filterName = value; }
        }

        /// <summary>
        /// Path to executable for new ISAPI Filter
        /// </summary>
        /// <value>a valid path to an ISAPI dll available on the Web Server</value>
        /// <remarks>required</remarks>
        public string FilterPath
        {
            get { return (this.filterPath ?? String.Empty); }
            set { this.filterPath = value; }
        }

        /// <summary>
        /// Description of web site to which we are adding the filter
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to apply the binding to</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }
            set { this.description = value; }
        }

        /// <summary>
        /// web site idenfitier for website to which we are adding the filter
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// </remarks>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }
            set { this.identifier = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.Description, this.Identifier, "AddFilter", this.MachineName);
            if (WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite web = WebSite.Load(this.MachineName, id);
                Microsoft.Sdc.Tasks.Configuration.Web.IsapiFilters filters = new Microsoft.Sdc.Tasks.Configuration.Web.IsapiFilters(web);
                filters.Add(this.FilterName, this.FilterPath);
                web.Save();
            }
        }
    }
}